/**
 *    @file       maintaincmd.h
 *
 *    @brief      
 *    @details
 *
 *    @version    1.0
 *    @date       02/17/2014 02:09:18 PM
 *
 *    @author     Theophilus (), wangtf418@gmail.com
 */
#ifndef _SF_MAINTAIINCMD_H_
#define _SF_MAINTAIINCMD_H_
#include "basecommand.h"

struct maintain_cmdhead
{
	bf_uint32_t m_version;
	bf_uint32_t m_sequence;
	bf_uint8_t m_cmdid;
	bf_uint32_t m_bodylen;
	//define macro
	enum
	{
		CMD_HEAD_LEN = 13,
		CMDID_OFFSET = 8
	};
	//member function
	static bf_int_t get_cmdid(bf_char_t *buf, bf_uint32_t buflen)
	{
		if (!buf || buflen < CMD_HEAD_LEN)
		{
			return -1;
		}
		bf_uint8_t cmdid;
		memcpy(&cmdid, buf + CMDID_OFFSET, sizeof(cmdid));
		return cmdid;
	}
	inline bf_int_t encode(buffermgr &bufmgr)
	{
		bf_int_t ret = 0;
		ret += bufmgr.put_int32(m_version);
		ret += bufmgr.put_int32(m_sequence);
		ret += bufmgr.put_int8(m_cmdid);
		ret += bufmgr.put_int32(m_bodylen);
		return (ret == 0)? BF_OK:BF_ERROR;
	}
	inline bf_int_t decode(buffermgr &bufmgr)
	{
		IF_ERR_RETURN(bufmgr.get_int32(m_version))
		IF_ERR_RETURN(bufmgr.get_int32(m_sequence))
		IF_ERR_RETURN(bufmgr.get_int8(m_cmdid))
		IF_ERR_RETURN(bufmgr.get_int32(m_bodylen))
		return BF_OK;
	}
	inline bf_uint32_t get_size()
	{
		return CMD_HEAD_LEN;
	}
	inline void set_bodylen(bf_uint32_t bodylen)
	{
		m_bodylen = bodylen;
	}
	inline bf_int_t verify_bodylen(bf_uint32_t buflen)
	{
		if ((m_bodylen + CMD_HEAD_LEN) != buflen)
		{
			return BF_ERROR;
		}
		return BF_OK;
	}
	inline bf_uint32_t get_cmdlen()
	{
		return (m_bodylen + CMD_HEAD_LEN);
	}
};

struct logswitch_cmdbody
{
	bf_int32_t m_logtype;
	bf_int8_t m_logflag;
	//member function
	inline bf_int_t encode(buffermgr &bufmgr)
	{
		bf_int_t ret = 0;
		ret += bufmgr.put_int32(m_logtype);
		ret += bufmgr.put_int8(m_logflag);
		return (ret == 0)? BF_OK:BF_ERROR;
	}
	inline bf_int_t decode(buffermgr &bufmgr)
	{
		IF_ERR_RETURN(bufmgr.get_int32(m_logtype))
		IF_ERR_RETURN(bufmgr.get_int8(m_logflag))
		return BF_OK;
	}
	bf_uint32_t get_size()
	{
		bf_uint32_t len = 0;
		len += sizeof(m_logtype);
		len += sizeof(m_logflag);
		return len;
	}
};

struct logswitchres_cmdbody
{
	bf_int8_t m_result;
	//member function
	inline bf_int_t encode(buffermgr &bufmgr)
	{
		bf_int_t ret = 0;
		ret += bufmgr.put_int8(m_result);
		return (ret == 0)? BF_OK:BF_ERROR;
	}
	inline bf_int_t decode(buffermgr &bufmgr)
	{
		IF_ERR_RETURN(bufmgr.get_int8(m_result))
		return BF_OK;
	}
	bf_uint32_t get_size()
	{
		bf_uint32_t len = 0;
		len += sizeof(m_result);
		return len;
	}
};

class logswitchcmd : public basecmd<maintain_cmdhead, logswitch_cmdbody, maintain_cmdhead, logswitchres_cmdbody>
{
public:
	logswitchcmd(bf_char_t *buf, bf_uint32_t buflen)
		: basecmd<maintain_cmdhead, logswitch_cmdbody, maintain_cmdhead, logswitchres_cmdbody>(buf, buflen)

	{
		m_res_cmdhead.m_cmdid = MAINTAIN_CMD_TYPE_LOGSWITCH_RES;
	}

	~logswitchcmd()
	{

	}

protected:
	virtual bf_int_t process_cmd();
};

#endif

